package com.cyt.sys.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.dao.SysNoticeDao;
import com.cyt.sys.model.po.SysNotice;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.vo.SysNoticeVo;
import com.cyt.sys.service.SysNoticeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 通知公告-通知公告Service层实现类
 *
 * @author rong yuan
 * @date 2019-8-16 15:19:42
 */
@Service("sysNoticeService")
public class SysNoticeServiceImpl extends BaseServiceImpl<SysNotice> implements SysNoticeService {

    @Resource
    private SysNoticeDao sysNoticeDao;

    @Autowired
    public void setBaseDao(SysNoticeDao sysNoticeDao) {
        this.baseDao = sysNoticeDao;
    }

    /**
     * 分页查询
     *
     * @param sysNoticeVo 接受参数对象
     * @param pageNum     分页数
     * @param pageSize    每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-16 15:19:42
     */
    @Override
    public ReturnResult page(SysNoticeVo sysNoticeVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<SysNoticeVo> list = sysNoticeDao.selectByCondition(sysNoticeVo);
        PageInfo<SysNoticeVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param sysNoticeVo 参数接受对象
     * @param sysUser     当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-16 15:19:42
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(SysNoticeVo sysNoticeVo, SysUser sysUser) {
        if (StringUtils.isBlank(sysNoticeVo.getId())) {
            SysNotice sysNotice = new SysNotice(sysUser);
            sysNotice.setTitle(sysNoticeVo.getTitle());
            sysNotice.setContent(sysNoticeVo.getContent());
            sysNotice.setReadNum(Integer.parseInt(sysNoticeVo.getReadNum()));
            sysNotice.setStatus(sysNoticeVo.getStatus());
            sysNoticeDao.insert(sysNotice);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            SysNotice sysNotice = sysNoticeDao.selectByPrimaryKey(sysNoticeVo.getId());
            sysNotice.setTitle(sysNoticeVo.getTitle());
            sysNotice.setContent(sysNoticeVo.getContent());
            sysNotice.setReadNum(Integer.parseInt(sysNoticeVo.getReadNum()));
            sysNotice.setStatus(sysNoticeVo.getStatus());
            sysNotice.setUpdateTime(new Date());
            sysNotice.setUpdateUser(sysUser.getId());
            sysNoticeDao.updateByPrimaryKey(sysNotice);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param sysNoticeVo 参数接受对象
     * @param sysUser     当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-16 15:19:42
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(SysNoticeVo sysNoticeVo, SysUser sysUser) {
        //要修改的字段
        SysNotice sysNotice = new SysNotice();
        sysNotice.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        sysNotice.setUpdateUser(sysUser.getId());
        sysNotice.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(SysNotice.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(sysNoticeVo.getId())) {
            criteria.andIn("id", Arrays.asList(sysNoticeVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        sysNoticeDao.updateByExampleSelective(sysNotice, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param sysNoticeVo 参数接受对象
     * @param sysUser     当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-16 15:19:42
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(SysNoticeVo sysNoticeVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(SysNotice.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(sysNoticeVo.getId())) {
            criteria.andIn("id", Arrays.asList(sysNoticeVo.getId().split(",")));
        }
        sysNoticeDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}